<?php
/**
 * Role.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/07
 * Time: 11:20
 */
namespace app\admin\controller;

use app\common\simple_rbac\Model;
use app\common\simple_rbac\Node;

class Role extends Base
{
    /**
     * @return mixed|\think\response\Json
     * @throws \Exception
     */
    public function save()
    {
        if($this->request->isPost())
        {
            $result = (new \app\api\open\role\Save($this->request->post()))->index();
            return json(['code'=>200,'data'=>$result]);
        }else if($this->request->isGet())
        {
            $id = $this->request->get('id');
            $info = null;
            if(!empty($id))
            {
                $res1 = \app\api\open\role\Get::getInstance()->index(['id'=>$id]);
                if(empty($res1['id']))
                {
                    exit("查询结果为空");
                }
                $info = $res1;
                // 处理下用户roles
                $info['nodes'] = explode(",",trim($info['nodes'],","));
            }
            // 节点列表
            $nodeList = Model::getInstance()->getNodeList('id,pid,name,title');

            $this->assign('commParam',[
                'info' => &$info,
                'nodes' => Node::recursive($nodeList),
            ]);
            return $this->fetch('auth/simple_rbac/role/save');
        }
        exit("error");
    }
}